<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

class GoodsContentFile extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 商品关联
     */
    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id');
    }

    /**
     * 处理文件URL授权
     * @param string $fileUrl 文件URL
     * @param bool $hasAuth 是否有权限
     * @return string 处理后的URL
     */
    public static function handleAuthFileUrl($fileUrl, $hasAuth = false)
    {
        return $hasAuth ? $fileUrl : '需要购买后获取下载链接';
    }

    /**
     * 获取指定商品的所有文件
     * @param int $goodsId 商品ID
     * @param bool $hasAuth 是否有权限查看文件URL
     * @return \think\Collection
     */
    public static function getGoodsFiles($goodsId, $hasAuth = false)
    {
        $files = self::where('goods_id', $goodsId)
            ->order('sort asc, id asc')
            ->select();

        if (!$hasAuth) {
            // 如果无权限查看，则隐藏文件URL
            foreach ($files as &$file) {
                $file->file_url = self::handleAuthFileUrl($file->file_url, false);
            }
        }

        return $files;
    }


    /**
     * 检查文件是否收费 - 简化逻辑
     * @return bool
     */
    public function isCharged()
    {
        // 如果文件设置为免费，则不收费
        if ($this->is_free) {
            return false;
        }

        // 默认情况下，根据商品价格判断
        return $this->goods && $this->goods->price > 0;
    }

    /**
     * 检查用户是否有权限下载文件
     * @param int $userId 用户ID
     * @return bool
     */
    public function canUserDownload($userId)
    {
        if (!$userId) {
            return false;
        }

        $user = User::find($userId);
        if (!$user) {
            return false;
        }

        // 如果文件不收费，则可以下载
        if (!$this->isCharged()) {
            return true;
        }

        return false;
    }


    /**
     * 更新文件内容 单文件
     * @param int $goodsId 商品ID
     * @param array $data 数据
     * @return \think\Model
     */
    public static function createOrUpdateFile($goodsId, $data)
    {
        $contentManager = self::where('goods_id', $goodsId)->find();
        if (!$contentManager) {
            $contentManager           = new self();
            $contentManager->goods_id = $goodsId;
        }
        $contentManager->save($data);
        return $contentManager;
    }

    /**
     * 更新文件内容 多文件
     * @param int $goodsId 商品ID
     * @param array $data 数据
     * @return \think\Model
     */
    public static function createOrUpdateFiles($goodsId, $data)
    {
        $contentManager = self::where('goods_id', $goodsId)->find();
        if (!$contentManager) {
            $contentManager           = new self();
            $contentManager->goods_id = $goodsId;
        }
        $contentManager->save($data);
        return $contentManager;
    }
}